//
//  GrowthRecordViewController.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/26.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "GrowthRecordViewController.h"
#import "AddGrowthRecordViewController.h"
#import "GrowthRecordTableViewCell.h"
#import "GrowthRecordModel.h"
#import "AdjustHeight.h"

@interface GrowthRecordViewController ()<UITableViewDelegate,UITableViewDataSource,DeleteCellDelegate>
@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;

@end

@implementation GrowthRecordViewController
{
    NSInteger page;
}
-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}
-(UITableView *)listV
{
    if (!_listV)
    {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.backgroundColor = [UIColor whiteColor];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        _listV.separatorStyle = 0;
        [_listV registerClass:[GrowthRecordTableViewCell class] forCellReuseIdentifier:@"GrowthRecordTableViewCell"];
    }
    return _listV;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"成长记录";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    [self.view addSubview:self.listV];
    [self Request];
    UIButton *rightbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    rightbutton.frame= CGRectMake(0, 10, 44, 44);
    rightbutton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
    [rightbutton addTarget:self action:@selector(rightOnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarbutton = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    [rightbutton setImage:[[UIImage imageNamed:@"publish"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
    self.navigationItem.rightBarButtonItem = rightBarbutton;

    self.listV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        page = 1;
        [self Request];
    }];

    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self Request];
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.listArr.count == 0) {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GrowthRecordModel *model = self.listArr[indexPath.row];
    GrowthRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GrowthRecordTableViewCell" forIndexPath:indexPath];
    NSString *month = [model.addtimedesc substringFromIndex:5];
    NSString *year = [model.addtimedesc substringToIndex:4];
    cell.YeartimeL.text = year;
    cell.dateL.text = month;
    cell.Delegate = self;
    cell.titleL.attributedText = [AttributeString string:model.adduser LineSpace:1 font:16 color:UIColors(0x333333) worldSpace:1];
    if ([model.adduser isEqualToString:@"老师发布"]) {
        [cell.DeleteBtn setHidden:YES];
    }else
    {
        [cell.DeleteBtn setHidden:NO];
    }
    cell.detailL.attributedText = [AttributeString string:model.contents LineSpace:1 font:14 color:UIColors(0x666666) worldSpace:1];

    NSMutableArray *PicArr = [NSMutableArray array];

    for (NSString *smallurl in model.thumbs) {
        [PicArr addObject:[smallurl stringByReplacingOccurrencesOfString:@"/thumb" withString:@""]];
    }
    if (indexPath.row == 0)
    {
        [cell.TopLine setHidden:YES];
         [cell.bottomline setHidden:YES];
    }else
    {
        [cell.TopLine setHidden:NO];
        [cell.bottomline setHidden:NO];
    }

    if (indexPath.row == self.listArr.count-1)
    {
         [cell.bottomline setHidden:YES];
    }else
    {
        [cell.bottomline setHidden:NO];
    }

    cell.listArr = PicArr;
    [cell.imageCollectionV reloadData];
    cell.selectionStyle = 0;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //+model.contents+cell高度
    GrowthRecordModel *model = self.listArr[indexPath.row];

    if (model.thumbs.count==0)
    {
        return 20+14+10+[AdjustHeight adjustHeightByString:model.contents width:KscreenW-80 font:14];
    }else
    {
        return  80+((model.thumbs.count-1)/3+1)*(80+8)+[AdjustHeight adjustHeightByString:model.contents width:KscreenW-80 font:14];
    }
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentGrowth&a=growuplogs" parmDic:@{@"sid":self.sid,@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {

        NSArray *Arr = [GrowthRecordModel ModelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }

        [self.listArr addObjectsFromArray:Arr];

        [self.listV reloadData];

        [self.listV.mj_footer endRefreshing];

        [self.listV.mj_header endRefreshing];

        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }

    } error:^(NSError *error)
    {

    }];
}

-(void)rightOnClick
{
    AddGrowthRecordViewController *vc = [[AddGrowthRecordViewController alloc]init];
    vc.sid = self.sid;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)DeleteCell:(UIButton *)btn
{
    GrowthRecordTableViewCell *cell = (GrowthRecordTableViewCell *)btn.superview.superview;
    NSIndexPath *indexpath = [self.listV indexPathForCell:cell];
    GrowthRecordModel *model = self.listArr[indexpath.row];

    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentGrowth&a=deletelog" parmDic:@{@"grid":model.grid} requestType:RequestPOST Success:^(id data)
    {
        [self.listArr removeObjectAtIndex:indexpath.row];
        [self.listV deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:(UITableViewRowAnimationFade)];
    } error:^(NSError *error)
    {
        
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
